﻿using System;
using System.Collections.Generic;
using System.Text;

namespace Pixysoft.Framework.Configurations.Core
{
    /// <summary>
    /// 针对xmlnodescollection的子集合进行操作 防止指针丢失
    /// </summary>
    class XmlNodesCollection : IXmlNodesCollection
    {
        int index = -1;

        List<IXmlNode> parent = null;

        List<IXmlNode> nodes = null;

        public XmlNodesCollection(List<IXmlNode> parent, List<IXmlNode> nodes)
        {
            this.parent = parent;
            this.nodes = nodes;
        }

        public IXmlNode Add(string elementName)
        {
            if (string.IsNullOrEmpty(elementName))
                throw new Exception("missing elementName.");

            elementName = elementName.Trim();


            //nested 允许重复

            //if (Contains(elementName))
            //    throw new Exception("node with ElementName already existed. ElementName = " + elementName);


            IXmlNode node = new XmlNode(elementName);

            nodes.Add(node);

            parent.Add(node);

            return node;
        }

        public IXmlNode Add(string elementName, string text)
        {
            if (string.IsNullOrEmpty(elementName))
                throw new Exception("missing elementName.");

            elementName = elementName.Trim();

            IXmlNode node = new XmlNode(elementName);

            node.Text = text;

            nodes.Add(node);

            parent.Add(node);

            return node;
        }

        public void Add(IXmlNode node)
        {
            if (node == null)
                throw new Exception("missing node.");

            if (string.IsNullOrEmpty(node.ElementName))
                throw new Exception("missing ElementName for node.");

            parent.Add(node);

            nodes.Add(node);
        }

        public IXmlNode this[int index]
        {
            get
            {
                if (index < 0 || index > nodes.Count - 1)
                    throw new Exception("index exceed nodes.count = " + nodes.Count);

                return nodes[index];
            }
        }

        public IXmlNodesCollection this[string elementName]
        {
            get
            {
                if (string.IsNullOrEmpty(elementName))
                    throw new Exception("missing elementname.");

                elementName = elementName.Trim();

                List<IXmlNode> list = new List<IXmlNode>();

                foreach (XmlNode node in nodes)
                {
                    if (string.Equals(node.ElementName, elementName, StringComparison.OrdinalIgnoreCase))
                        list.Add(node);
                }

                return new XmlNodesCollection(parent, list);
            }
        }

        public IXmlNodesCollection Remove(string elementName)
        {
            IXmlNodesCollection collection = this[elementName];

            foreach (IXmlNode node in collection)
            {
                nodes.Remove(node);

                parent.Remove(node);
            }

            return collection;
        }

        public bool Contains(string elementName)
        {
            if (string.IsNullOrEmpty(elementName))
                return false;

            elementName = elementName.Trim();

            foreach (XmlNode node in nodes)
            {
                if (string.Equals(node.ElementName, elementName, StringComparison.OrdinalIgnoreCase))
                    return true;
            }

            return false;
        }

        public int Count
        {
            get
            {
                return nodes.Count;
            }
        }

        public void Clear()
        {
            foreach (IXmlNode node in nodes)
            {
                parent.Remove(node);
            }

            nodes.Clear();
        }

        #region IEnumerable<IXmlNode> 成员

        public IEnumerator<IXmlNode> GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerable 成员

        System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()
        {
            return this;
        }

        #endregion

        #region IEnumerator<IXmlNode> 成员

        public IXmlNode Current
        {
            get
            {
                return this[index];
            }
        }

        #endregion

        #region IDisposable 成员

        public void Dispose()
        {
            Reset();
        }

        #endregion

        #region IEnumerator 成员

        object System.Collections.IEnumerator.Current
        {
            get
            {
                return this[index];
            }
        }

        public bool MoveNext()
        {
            if (index >= nodes.Count - 1)
                return false;

            index++;

            return true;
        }

        public void Reset()
        {
            index = -1;
        }

        #endregion

        public override string ToString()
        {
            return "node.count = " + nodes.Count;
        }
    }
}
